using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace LenninSoftware.FormEditor.Adapters
{
    class Number : BaseControlAdapter
    {

        public override Control CreateReadOnlyControl(FieldInfo field)
        {
            Label label = new Label();
            return label;
        }

        public override void LoadReadOnlyData(Control control, SPListItem item, object data)
        {
            Label label = (Label)control;

            if (data == null || !(data is double))
                label.Text = string.Empty;


            double value = (double)data;
            label.Text = value.ToString("G");
        }

        public override System.Web.UI.Control CreateControl(FieldInfo field)
        {
            TextBox texBox = new TextBox();
            return texBox;
        }

        public override void LoadData(System.Web.UI.Control control, SPListItem item, object data)
        {
            TextBox texBox1 = (TextBox)control;
            texBox1.Text = data == null ? String.Empty : data.ToString();

        }

        public override object SaveData(System.Web.UI.Control control, SPListItem item, object currentData)
        {
            TextBox texBox1 = (TextBox)control;

            string text = texBox1.Text;

            if (String.IsNullOrEmpty(text))
                return 0;

            double outputValue = 0;

            double.TryParse(text, out outputValue);

            return outputValue;
        }

        public override void Reset(System.Web.UI.Control control)
        {
            TextBox texBox = (TextBox)control;
            texBox.Text = String.Empty;
        }

        public override bool isEmpty(Control control)
        {
            TextBox texBox = (TextBox)control;
            return String.IsNullOrEmpty(texBox.Text);
        }
    }
}
